

using Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using SOEI.Solution.Customized;
using SOEI.Solution.FileUpLoader.DomainService;
using SOEI.Solution.Helper;
using SOEI.Solution.Integratedes.Organization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ThirdParty.ESignClient;

namespace SOEI.Solution.Integratedes.ElectronicSignet.DomainService
{
	/// <summary>
	/// 领域服务层一个模块的核心业务逻辑
	///</summary>
	public class OrgSignetManager :DomainServiceBase, IOrgSignetManager
	{
		
		private readonly IRepository<OrgSignet,long> _orgsignetRepository;

		private readonly IESignManager _eSignManager;

		private readonly IAttachManager _attachManager;

		/// <summary>
		/// 【OrgSignet】的构造方法
		/// 通过构造函数注册服务到依赖注入容器中
		///</summary>
		public OrgSignetManager(
				IRepository<OrgSignet, long> orgsignetRepository,
				IESignManager eSignManager,
				IAttachManager attachManager
			)	{
			_orgsignetRepository =  orgsignetRepository;
			_eSignManager = eSignManager;
			_attachManager = attachManager;
		}

		#region -------------------------------------------------辅助工具生成---------------------------------------------- 

		/// <summary>
		/// 返回列表查询用
		/// </summary>
		/// <returns></returns>
		public IQueryable<OrgSignet> QueryEntityListAsNoTracking() { 

			var query = _orgsignetRepository.GetAll().AsNoTracking()
						.Select(x => new OrgSignet
						{
						   
							JGBH = x.JGBH,
							RYBH = x.RYBH,
							AccountID = x.AccountID,
							ORGID = x.ORGID,
							IdentifyModuleID = x.IdentifyModuleID,
							LicenseModuleID = x.LicenseModuleID,
							SignetModuleID = x.SignetModuleID,
							Status = x.Status,
						});
			return query;
		}

		/// <summary>
		/// 【OrgSignet】返回表达式数的实体信息即IQueryable类型
		/// </summary>
		/// <returns>IQueryable</returns>
		public IQueryable<OrgSignet> Query()
		{
			return _orgsignetRepository.GetAll();
		}
		/// <summary>
		/// 【OrgSignet】返回即IQueryable类型的实体，不包含EF Core跟踪标记
		/// </summary>
		/// <returns>IQueryable</returns>
		public IQueryable<OrgSignet> QueryAsNoTracking()
		{
			return _orgsignetRepository.GetAll().AsNoTracking();
		}
		/// <summary>
		/// 【OrgSignet】根据Id查询实体信息
		/// </summary>
		/// <param name="id"></param>
		/// <returns>实体</returns>
		public async Task<OrgSignet> FindByIdAsync(long id)
		{
			var entity = await _orgsignetRepository.GetAsync(id);
			return entity;
		}
		/// <summary>
		/// 【OrgSignet】检查实体是否存在
		/// </summary>
		/// <param name="id"></param>
		/// <returns>bool</returns>
		public async Task<bool> IsExistAsync(long id)
		{
			var result = await _orgsignetRepository.GetAll().AnyAsync(a => a.Id == id);
			return result;
		}
		/// <summary>
		/// 【OrgSignet】创建实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task<OrgSignet> CreateAsync(OrgSignet entity)
		{
			entity.Id = await _orgsignetRepository.InsertAndGetIdAsync(entity);
			return entity;
		}
		/// <summary>
		/// 【OrgSignet】更新实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task UpdateAsync(OrgSignet entity)
		{
			await _orgsignetRepository.UpdateAsync(entity);
		}
		/// <summary>
		/// 【OrgSignet】删除实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task DeleteAsync(long id)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _orgsignetRepository.DeleteAsync(id);
		}
		/// <summary>
		/// 【OrgSignet】批量删除
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task BatchDelete(List<long> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _orgsignetRepository.DeleteAsync(a => input.Contains(a.Id));
		}
		#endregion


		#region -------------------------------------------------用户自定义------------------------------------------------
		/*请在此扩展领域服务接口*/
		/// <summary>
		/// 是否存在指定条件的印章实体
		/// </summary>
		/// <param name="JGBH">机构编号</param>
		/// <param name="RYBH">人员编号</param>
		/// <returns></returns>
		public async Task<bool> IsExistAsync(OEconomyOrgani organi)
		{
			var result = await _orgsignetRepository.GetAll().AnyAsync(a => a.JGBH == organi.Id && a.RYBH == organi.GLYBH);
			return result;
		}

		/// <summary>
		/// 获取一个电子签章数据，如果不存在则会创建一个
		/// </summary>
		/// <param name="JGBH"></param>
		/// <param name="RYBH"></param>
		/// <returns></returns>
		public async Task<OrgSignet> GetCreateOrgSignetAsync(OEconomyOrgani organi)
		{
			if (await this.IsExistAsync(organi))
			{
				var OrgSignetEntity = await this.Query().Where(a => a.JGBH == organi.Id && a.RYBH == organi.GLYBH).FirstOrDefaultAsync();
				// 获取身份证文件清单
				OrgSignetEntity.IdentifyAttachs = await _attachManager.GetAttachByModuleID(OrgSignetEntity.IdentifyModuleID);
				// 获取营业执照文件清单
				OrgSignetEntity.LicenseAttachs = await _attachManager.GetAttachByModuleID(OrgSignetEntity.LicenseModuleID);
				// 电子印章附件集合
				OrgSignetEntity.SignetAttachs = await _attachManager.GetAttachByModuleID(OrgSignetEntity.SignetModuleID);
				return OrgSignetEntity;
			}
			else
			{
				var OrgSignetEntity = EntityHelper.GetNewOrgSignet();
				OrgSignetEntity.JGBH = organi.Id;
				OrgSignetEntity.RYBH = organi.GLYBH;
				OrgSignetEntity.LicenseModuleID = this.GetAttachGuid(organi.LicenseModuleID);   // 主要解决老数据问题
				OrgSignetEntity.IdentifyModuleID = this.GetAttachGuid(organi.IdentifyModuleID);
				// 获取身份证文件清单
				OrgSignetEntity.IdentifyAttachs = await _attachManager.GetAttachByModuleID(OrgSignetEntity.IdentifyModuleID);
				// 获取营业执照文件清单
				OrgSignetEntity.LicenseAttachs = await _attachManager.GetAttachByModuleID(OrgSignetEntity.LicenseModuleID);
				OrgSignetEntity = await this.CreateAsync(OrgSignetEntity);
				return OrgSignetEntity;
			}
		}

		/// <summary>
		/// 【电子签章】根据管理人员编号查询实体信息
		/// </summary>
		/// <param name="id"></param>
		/// <returns>实体</returns>
		public async Task<OrgSignet> FindByPersonIDAsync(string RYBH)
		{
			var entity = await this.QueryAsNoTracking().Where(x => x.RYBH == RYBH).FirstOrDefaultAsync();
			return entity;
		}

		/// <summary>
		/// 【电子签章】根据机构编号查询实体信息
		/// </summary>
		/// <param name="OrgID"></param>
		/// <returns></returns>
		public async Task<OrgSignet> FindByOrgIDAsync(string OrgID)
		{
			var entity = await this._orgsignetRepository.GetAll().Where(x => x.JGBH == OrgID).FirstOrDefaultAsync();

			return entity;
		}

		/// <summary>
		/// 【电子签章】指定机构编是否可以进行签章申请
		///  1. 没有电子签章申请记录的 可以进行申请
		///  2. 审核状态为待提交记录的 可以进行申请
		///  3. 暂时其他状态都不允许在进行申请
		/// </summary>
		/// <param name="OrgID"></param>
		/// <returns></returns>
		public async Task<bool> UseSignet(string OrgID)
		{
			var entity = await this.FindByOrgIDAsync(OrgID);
			if (entity == null) return true; // 如果找不到数据则可以备案 
			if (entity.Status == ((int)EApprovalStatus.WaitSubmit).ToString())
			{

				return true;
			}

			return false;
		}

		#endregion










	}
}
